package com.system.controller;

import com.system.pojo.Contract;
import com.system.pojo.ContractGoods;
import com.system.pojo.Invoice;
import com.system.pojo.SysUser;
import com.system.service.InvoiceService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@ResponseBody
@RequestMapping("/invoice")
public class InvoiceController {

    @Autowired
    private InvoiceService invoiceService;

    /**
     * 发货单发货
     * @param session
     * @param invoiceId
     * @return
     */
    @RequestMapping("/update/{invoiceId}")
    public ResultVo<Invoice> update(HttpSession session, @PathVariable Integer invoiceId) {
        ResultVo<Invoice> resultVo = new ResultVo<>();
        Object sysUser = session.getAttribute("sysUser");
        try {
            if (sysUser == null) {
                resultVo.setMessage("发货失败");
                return resultVo;
            } else {
                SysUser user = (SysUser) sysUser;
                Integer uid = user.getId();
                resultVo = invoiceService.deliver(uid, invoiceId);
//                resultVo.setMessage("发货成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("发货失败");
        }finally {
            return resultVo;
        }
    }
    
    @RequestMapping(value = "/updateInvoice",method = RequestMethod.POST)
    public ResultVo<Invoice> updateInvoice (@RequestBody Invoice invoice){
    	ResultVo<Invoice> resultVo = new ResultVo<Invoice>();
    	try {
			resultVo = invoiceService.updateInvoice(invoice);
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setMessage("修改失败");
		}finally {
			return resultVo;
		}
    }

    @RequestMapping("/selectAllUndone")
    public ResultVo<Invoice> selectAllUndone() {
        ResultVo<Invoice> resultVo = new ResultVo<Invoice>();
        try {
            resultVo = invoiceService.selectAllUndone();
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/selectAllDone")
    public ResultVo<Invoice> selectAllDone() {
        ResultVo<Invoice> resultVo = new ResultVo<Invoice>();
        try {
            resultVo = invoiceService.selectAllDone();
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("查询失败");
        }finally {
            return resultVo;
        }
    }

    /*@RequestMapping("/contractInvoices/{contractId}")
    public ResultVo<Invoice> contractInvoices(@PathVariable Integer contractId){
        ResultVo<Invoice> resultVo = null;
        try {
            resultVo = invoiceService.contractInvoices(contractId);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("查询购买的产品失败");
        }finally {
            return resultVo;
        }
    }*/

    @RequestMapping("/addproduct")
    public ResultVo<Invoice> addproduct(@RequestBody Contract contract){
        ResultVo<Invoice> resultVo = null;
        try {
            resultVo = null;
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("查询购买的产品失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResultVo<Invoice> deleteById(@RequestBody Invoice invoice){
        ResultVo<Invoice> resultVo = new ResultVo<Invoice>();
        try {
            resultVo = invoiceService.delete(invoice);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("删除失败");
        }finally {
            return resultVo;
        }
    }

    @RequestMapping("/insert")
    public ResultVo<Invoice> insert(@RequestBody ContractGoods cg){
        ResultVo<Invoice> resultVo = new ResultVo<>();
        try {
            resultVo = invoiceService.insert(cg);
        } catch (Exception e) {
            e.printStackTrace();
            resultVo.setMessage("发货失败");
        }finally {
            return resultVo;
        }
    }
}
